package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Apply;
import com.example.demo.entity.Demand;
import com.example.demo.entity.User;
import com.example.demo.entity.vo.ApplyVo;
import com.example.demo.entity.vo.DemandVo;
import com.example.demo.exception.bizException.BizException;
import com.example.demo.mapper.ApplyMapper;
import com.example.demo.service.ApplyService;
import com.example.demo.service.DemandService;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* <p>
*  服务实现类
* </p>
*
* @author cy
* @since 2020-08-01
*/
@Slf4j
@Service
public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements ApplyService {

    @Autowired
    private UserService userService;

    @Autowired
    private DemandService demandService;

    @Override
    public Page<ApplyVo> listApplysByPage(int page, int pageSize, Integer  dId,Integer uId) {
        log.info("正在执行分页查询apply: page = {} pageSize = {} dId = {} uId={}",page,pageSize,dId,uId);
        QueryWrapper<Apply> queryWrapper =  new QueryWrapper<Apply>().eq(dId!=null,"d_id",dId)
                .eq(uId!=null,"u_id",uId);
        Page<Apply> result = super.page(new Page<>(page, pageSize),queryWrapper);
        //转为vo，插入用户数据
        Page<ApplyVo> voPage=new Page<>();
        List<ApplyVo> voList=new ArrayList<>(result.getRecords().size());
        for(Apply apply:result.getRecords()){
            ApplyVo applyVo=new ApplyVo();
            applyVo.setApply(apply);
            //去除密码
            QueryWrapper<User> userQueryWrapper=new QueryWrapper<User>().select(User.class,info-> !"password".equals(info.getColumn())).
                    eq("user_id",apply.getUId());
            applyVo.setUser(userService.getByQueryWrapper(userQueryWrapper));
            voList.add(applyVo);
        }
        BeanUtils.copyProperties(result,voPage);
        voPage.setRecords(voList);
        log.info("分页查询apply完毕: 结果数 = {} ",result.getRecords().size());
        return voPage;
    }

    @Override
    public Page<ApplyVo> listApplysById(Integer uId) {
        QueryWrapper<Apply> queryWrapper =  new QueryWrapper<Apply>().eq("u_id",uId);
        Page<Apply> result = super.page(new Page<>(1, 20),queryWrapper);
        //转为vo，插入用户数据
        Page<ApplyVo> voPage=new Page<>();
        List<ApplyVo> voList=new ArrayList<>(result.getRecords().size());
        for(Apply apply:result.getRecords()){
            ApplyVo applyVo=new ApplyVo();
            applyVo.setApply(apply);
            //去除密码
            QueryWrapper<User> userQueryWrapper=new QueryWrapper<User>().select(User.class,info-> !"password".equals(info.getColumn())).
                    eq("user_id",apply.getUId());
            applyVo.setUser(userService.getByQueryWrapper(userQueryWrapper));
            voList.add(applyVo);
        }
        BeanUtils.copyProperties(result,voPage);
        voPage.setRecords(voList);
        log.info("分页查询apply完毕: 结果数 = {} ",result.getRecords().size());
        return voPage;
    }

    @Override
    public Apply getApplyById(int id) {
        log.info("正在查询apply中id为{}的数据",id);
        Apply apply = super.getById(id);
        log.info("查询id为{}的apply{}",id,(null == apply?"无结果":"成功"));
        return apply;
    }

    @Override
    public int insertApply(Apply apply) {
        log.info("正在插入apply");
        if(super.getOne(new QueryWrapper<Apply>().eq("d_id",apply.getDId()).eq("u_id",apply.getUId()))!=null){
            throw new BizException("用户已报名过此需求");
        }
        if(demandService.getDemandById(apply.getDId()).getUser().getUserId().equals(apply.getUId())){
            throw new BizException("不可报名自己的需求");
        }
        //设置状态
        apply.setStatus(Apply.CHECK);
        if (super.save(apply)) {
            log.info("插入apply成功,id为{}",apply.getApplyId());
            return apply.getApplyId();
        } else {
            log.error("插入apply失败");
            throw new BizException("添加失败");
        }
    }

    @Transactional
    @Override
    public int deleteApplyById(int id,int userId) {
        log.info("正在删除id为{}的apply",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的apply成功",id);
            return id;
        } else {
            log.error("删除id为{}的apply失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Transactional
    @Override
    public int updateApply(Apply apply) {
        log.info("正在更新id为{}的apply",apply.getApplyId());
        if (super.updateById(apply)) {
            log.info("更新d为{}的apply成功",apply.getApplyId());
            if(apply.getStatus() != null && apply.getDId() != null && apply.getStatus() == 2) {
                DemandVo demandVo = demandService.getDemandById(apply.getDId());
                Demand demand = demandVo.getDemand();
                demand.setState(2);
                demandService.updateDemand(demand);
            }
            return apply.getApplyId();
        } else {
            log.error("更新id为{}的apply失败",apply.getApplyId());
            throw new BizException("更新失败[id=" + apply.getApplyId() + "]");
        }
    }


}
